﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using Aden.Util.Common;
using Aden.Model.Net;
using Aden.Model.Stock;
using Aden.BLL.StockData;

namespace AdenWebAPI.Controllers
{
    public class StockDataController : BaseController
    {
        /// <summary>
        /// 供应商 - 日期组合
        /// </summary>
        /// <param name="token"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public Response SupplierDate(string token, [FromBody]StockParam param)
        {
            Response response = new Response();
            if (string.IsNullOrEmpty(token) || !token.Equals(_token))
            {
                response.code = "404";
                response.message = "Invalid token";
            }
            else
            {
                var data = StockDataHelper.SupplierDate(param);
                if (data == null || data.Count == 0)
                {
                    response.code = "500";
                    response.message = "No Data";
                }
                else
                {
                    response.code = "200";
                    response.content = data;
                }
            }
            return response;
        }
        /// <summary>
        /// 库存管理的四种类型：入库、出库、消耗、转仓
        /// </summary>
        /// <param name="token"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public Response GetOrderType(string token, [FromBody]OrderType param)
        {
            Response response = new Response();
            if(string.IsNullOrWhiteSpace(token) || !token.Equals(_token))
            {
                response.code = "404";
                response.message = "Invalid token";
            }
            else
            {
                var data = StockDataHelper.GetOrderType(param);
                if(data == null || data.Count == 0)
                {
                    response.code = "500";
                    response.message = "No Data";
                }
                else
                {
                    response.code = "200";
                    response.content = data;
                }
            }
            return response;
        }
        /// <summary>
        /// 库存明细列表
        /// </summary>
        /// <param name="token"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public Response GetTransactionData(string token, [FromBody]StockParam param)
        {
            Response response = new Response();
            if(string.IsNullOrWhiteSpace(token) || !token.Equals(_token))
            {
                response.code = "404";
                response.message = "Invalid token";
            }
            else
            {
                var data = StockDataHelper.GetTransactionDataList(param.date.ToDateTime(), param.costCenterCode,
                param.supplierCode, param.customerCode, param.coa,  param.orderType,param.appType, param.productDesc);
                if(data == null || data.Count == 0)
                {
                    response.code = "500";
                    response.message = "No Data";
                }
                else
                {
                    response.code = "200";
                    response.content = data;
                }
            }
            return response;
        }
        /// <summary>
        /// 上传库存数据
        /// </summary>
        /// <param name="token"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public Response SaveToTransactionData(string token, [FromBody]StockParam param)
        {
            Response response = new Response();
            if(string.IsNullOrWhiteSpace(token) || !token.Equals(_token))
            {
                response.code = "404";
                response.message = "Invalid Token";
            }
            else
            {
                int intExCount = 0;
                intExCount = StockDataHelper.SaveToTransactionData(param);
                if (intExCount == 0)
                {
                    response.code = "500";
                    response.message = "Fail to Save!";
                } 
                else
                {
                    response.code = "200";
                }
            }
            return response;
        }
        /// <summary>
        /// 库存消耗 - 早、中、晚餐下拉框选项
        /// </summary>
        /// <param name="token"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public Response GetProductDescOptions(string token, [FromBody] StockParam param)
        {
            Response response = new Response();
            if (string.IsNullOrWhiteSpace(token) || !token.Equals(_token))
            {
                response.code = "404";
                response.message = "Invalid Token";
            }
            else
            {
                var data = StockDataHelper.GetProductDescOptions(param);
                if (data == null)
                {
                    response.code = "500";
                    response.message = "No Data";
                }
                else
                {
                    response.code = "200";
                    response.content = data;
                }
            }
            return response;
        }
        
    }
}